package com.linkoog.devtools.action_test;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiClassType;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiIdentifier;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.PsiReferenceExpression;
import com.intellij.psi.PsiType;
import com.intellij.psi.PsiTypeParameter;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTypesUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.PsiUtilCore;
import com.linkoog.devtools.http.parser.spring.SpringHttpServiceParser;
import com.linkoog.devtools.http.raw_index.HttpServiceMethod;
import com.linkoog.devtools.metadata.raw_index.ClassMetadata;
import com.linkoog.devtools.metadata.raw_index.ClassMetadataRawFileIndex;
import com.linkoog.devtools.type.Type;
import com.linkoog.devtools.type.TypeParser;
import com.linkoog.devtools.type.javadoc.JavaDoc;
import com.linkoog.devtools.type.javadoc.JavaDocParser;
import com.linkoog.devtools.type.swagger.JavaSwaggerParser;
import org.jetbrains.annotations.NotNull;

public class TypeParserAction extends AnAction {

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        final Project project = e.getProject();
        if (project == null) return;

        PsiElement element = e.getData(CommonDataKeys.PSI_ELEMENT);
        PsiMethod psiMethod = getPsiMethod(element);
        if (psiMethod == null) return;

        ApplicationManager.getApplication().runReadAction(() -> {
            Type returnType = TypeParser.parse(psiMethod.getReturnType());

            System.out.println("=====" + psiMethod.getName());
            PsiParameter[] parameters = psiMethod.getParameterList().getParameters();
            for (PsiParameter parameter : parameters) {
                Type paramType = TypeParser.parse(parameter.getType());

                System.out.println("=====" + parameter.getName());
            }

            PsiTypeParameter[] typeParameters = psiMethod.getTypeParameters();
            for (PsiTypeParameter typeParameter : typeParameters) {
                System.out.println("=====" + typeParameter.getName());
            }

            final HttpServiceMethod httpServiceMethod = SpringHttpServiceParser.getInstance().parse(psiMethod);
            final JavaDoc methodJavaDoc = JavaDocParser.parse(psiMethod.getDocComment());
            final String swaggerComment = JavaSwaggerParser.parseMethod(psiMethod);
            System.out.println(httpServiceMethod);
        });







    }

    /**
     * 解析当前光标选中的元素, 返回 PsiMethod.
     *
     * @param element element
     * @since 1.1.0
     */
    public static PsiMethod getPsiMethod(PsiElement element) {
        PsiMethod method = null;
        if (element instanceof PsiMethod) {
            method = (PsiMethod) element;
        } else if (element instanceof PsiIdentifier) {
            // 如果标识符是方法
            if (element.getParent() instanceof PsiMethod) {
                method = (PsiMethod) element.getParent();
            } else if (element.getParent() instanceof PsiReferenceExpression) {
                // 如果标识符是方法引用
                PsiElement resolve = ((PsiReferenceExpression) element.getParent()).resolve();
                if (resolve instanceof PsiMethod) {
                    method = (PsiMethod) resolve;
                }
            }
        }
        return method;
    }
}
